﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Presentation.UsersAndSecurity.IViews;
using Innova.FoundationFramework.DataTypes;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.Business.UsersAndSecurity;

namespace MyCommerce.Presentation.UsersAndSecurity.Presenters
{
    public class UserAdministrationPresenter
    {
        private IUserAdministrationView pViewToPresent;

        public UserAdministrationPresenter(IUserAdministrationView vViewToPresent)
        {
            pViewToPresent = vViewToPresent;
            pViewToPresent.OnCreateUser += pViewToPresent_OnCreateUser;
            pViewToPresent.OnDeleteUser += pViewToPresent_OnDeleteUser;
            pViewToPresent.OnEditUser += new GenericEventHandler<User>(pViewToPresent_OnEditUser);
            pViewToPresent.OnLoadView += pViewToPresent_OnLoadView;
            pViewToPresent.OnGeneratedPassword += new EventHandler(pViewToPresent_OnGeneratedPassword);
            pViewToPresent.OnChangePassword += new GenericEventHandler<List<string>>(pViewToPresent_OnChangePassword);
        }

        private void pViewToPresent_OnChangePassword(object sender, GenericEventArgs<List<string>> e)
        {
            try
            {
                UserManager userMgr = new UserManager(true);
                userMgr.ChangeUserPassword(pViewToPresent.ObjUserSelected.IdUser, e.EventData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void pViewToPresent_OnGeneratedPassword(object sender, EventArgs e)
        {
            try
            {
                UserManager userMgr = new UserManager(true);
                userMgr.GeneratedNewPassword(pViewToPresent.ObjUserSelected.IdUser);                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void pViewToPresent_OnLoadView(object sender, GenericEventArgs<long> e)
        {
            LoadUserList(e.EventData);
        }

        private void LoadUserList(long idUser)
        {
            UserManager userMgr = new UserManager();
            pViewToPresent.ColUsersList = userMgr.GetUsersList(idUser);
        }

        private void pViewToPresent_OnEditUser(object sender, GenericEventArgs<User> e)
        {
            bool result = pViewToPresent.InvokeEditionView(e.EventData);
            if (result)
                LoadUserList(pViewToPresent.UserId);
        }

        private void pViewToPresent_OnDeleteUser(object sender, GenericEventArgs<User> e)
        {
            try
            {
                UserManager userMgr = new UserManager(true);
                userMgr.DeleteUser(e.EventData);
                LoadUserList(pViewToPresent.UserId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void pViewToPresent_OnCreateUser(object sender, EventArgs e)
        {
            bool result = pViewToPresent.InvokeEditionView(null);
            if (result)
                LoadUserList(pViewToPresent.UserId);
        }
    }
}
